package org.yunmai.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.yunmai.dto.PermissionPageQuery;
import org.yunmai.entity.SysPermission;
import org.yunmai.service.SysPermissionService;
import org.yunmai.util.R;
import org.yunmai.vo.PageResult;
import org.yunmai.vo.SysPermissionVO;

import java.util.List;

/**
 * @Author 李孝辉
 * @Date 2025/6/3 22:19
 * @email 1626285873@qq.com
 * @Phone 18724505266
 * @内容
 */
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {

    @Resource
    SysPermissionService sysPermissionService;

    @GetMapping("/showAll")
    public R<List<SysPermission>> showAll(){
        return R.success(sysPermissionService.list());
    }

    @PostMapping("/getById")
    public R<SysPermission> getById(Integer id) {
        return R.success(sysPermissionService.getById(id));
    }

    @PostMapping("/deleteById")
    public R<Boolean> deleteById(Integer id) {
        return R.success(sysPermissionService.removeById(id));
    }

    @PutMapping("/update")
    public R<Boolean> update(@RequestBody SysPermission sysPermission) {
        if (sysPermission.getId() == null) {
            return R.error("id不能为空");
        }
        boolean result = sysPermissionService.updateById(sysPermission);
        return R.success(result);
    }

    @PostMapping("/PageList")//用于分页查询permission
    public R<PageResult<SysPermissionVO>> getPermissionPageList(@RequestBody PermissionPageQuery query) {
        List<SysPermissionVO> permissions = sysPermissionService.getPermissionListByRoleId(query.getPageNum(), query.getPageSize(), query.getKeyword());
        int total = permissions.toArray().length;
        PageResult<SysPermissionVO> result = new PageResult<>();
        result.setList(permissions);
        result.setTotal(total);
        return R.success(result);
    }
}
